/**
 * Created by zhang on 2016/1/20.
 */
ige.service('EmployeeService', ['$mdDialog', '$http', '$cookieStore', function ($mdDialog, $http, $cookieStore) {
    var showErrorDlg = function (title, content) {
        $mdDialog.show(
            $mdDialog.alert()
                .clickOutsideToClose(true)
                .title(title)
                .textContent(content)
                .ok('确定')
        );
    };
    var headers = {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        Authorization: $cookieStore.get('access_token')
    };

    var listApi = ige.systemSetting.apiDomain + 'web/employee/list';
    var deleteApi = ige.systemSetting.apiDomain + 'web/employee/delete';
    var addApi = ige.systemSetting.apiDomain + 'web/employee/add';
    var editApi = ige.systemSetting.apiDomain + 'web/employee/update';
    var getApi = ige.systemSetting.apiDomain + 'web/employee/detail';
    var progressApi = ige.systemSetting.apiDomain + 'web/calculate/getcalculate';
    var calculateApi = ige.systemSetting.apiDomain + 'web/calculate/calculatedata';
    var bestschemeApi = ige.systemSetting.apiDomain + 'web/calculate/bestscheme';
    var bestschemesApi = ige.systemSetting.apiDomain + 'web/calculate/bestschemes';
    var downloadApi = ige.systemSetting.apiDomain + 'web/calculate/download';

    var bestschemeApi = ige.systemSetting.apiDomain + 'web/calculate/bestscheme';
    var bestschemesApi = ige.systemSetting.apiDomain + 'web/calculate/bestschemes';
    var downloadApi = ige.systemSetting.apiDomain + 'web/calculate/download';

    var result = {
        getEmployees: function (pageSkip, pageSize, filter, successCallback) {
            var promise = $http.post(listApi,
                {
                    pageSkip: pageSkip,
                    pageSize: pageSize,
                    filter: filter
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback(response.data.data)
                } else {
                    showErrorDlg('获取员工出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('获取员工出错', response.statusText);
            });

            return promise;
        },
        getEmployee: function (id, successCallback) {
            var promise = $http.post(getApi,
                {
                    id: id
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback(response.data.data)
                } else {
                    showErrorDlg('获取员工出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('获取员工出错', response.statusText);
            });
            return promise;
        },
        deleteEmployee: function (id, successCallback) {
            var promise = $http.post(deleteApi,
                {id: id}, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('删除出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('删除出错', response.statusText);
            });
            return promise;
        },
        addEmployee: function (employee, successCallback) {
            var promise = $http.post(addApi,
                {
                    name: employee.Name,
                    phone: employee.Phone,
                    email: employee.Email,
                    departmentid: employee.DepartmentId,
                    description: employee.Description,
                    password: employee.Password,
                    salary: employee.Salary,
                    reward: employee.Reward,
                    insurances: employee.Insurances,
                    adjust: employee.Adjust
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('添加员工出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('添加员工出错', response.statusText);
            });
            return promise;
        },
        editEmployee: function (employee, successCallback) {
            var promise = $http.post(editApi,
                {
                    id: employee.Id,
                    name: employee.Name,
                    phone: employee.Phone,
                    email: employee.Email,
                    description: employee.Description,
                    departmentid: employee.DepartmentId,
                    password: employee.Password,
                    salary: employee.Salary,
                    reward: employee.Reward,
                    insurances: employee.Insurances,
                    adjust: employee.Adjust
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('编辑员工出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('编辑员工出错', response.statusText);
            });
            return promise;
        },
        computerReward: function (employeeId, rewardId, successCallback) {
            var promise = $http.post(calculateApi,
                {
                    employeeid: employeeId,
                    salar_scheme_yid: rewardId
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback(response.data.data)
                } else {
                    showErrorDlg('初始化计算出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('初始化计算出错', response.statusText);
            });
            return promise;
        },
        getRewardProgress: function (skip, size, progressCallback, successCallback) {
            var promise = $http.post(progressApi,
                {skip: skip, size: size}, {
                    headers: headers
                }).then(function (response) {
                if (response.data.code == 202) {
                    progressCallback(response.data.data);
                } else if (response.data.code == 200) {
                    successCallback()
                } else {
                    showErrorDlg('计算出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('计算出错', response.statusText);
            });
            return promise;
        },
        getbestschemeApi: function (employeeId, rewardId, resultMethodId, rewardAvgId, successCallback) {
            var promise = $http.post(bestschemeApi,
                {
                    rewardAvgId: rewardAvgId,
                    employeeid: employeeId,
                    salar_scheme_yid: rewardId,
                    filter: resultMethodId
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.code == 200) {
                    successCallback(response.data.data)
                } else {
                    showErrorDlg('计算出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('计算出错', response.statusText);
            });
            return promise;
        },
        getbestschemesApi: function (employeeIds, rewardId, resultMethodId, rewardAvgId, successCallback) {
            var promise = $http.post(bestschemesApi,
                {
                    rewardAvgId: rewardAvgId,
                    employeeid: employeeIds,
                    salar_scheme_yid: rewardId,
                    filter: resultMethodId
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.code == 200) {
                    successCallback(response.data.data)
                } else {
                    showErrorDlg('计算出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('计算出错', response.statusText);
            });
            return promise;
        },
        downLoadApi: function (employeeIds, rewardId, resultMethodId, successCallback) {
            var promise = $http.post(downloadApi,
                {
                    employeeid: employeeIds,
                    salar_scheme_yid: rewardId,
                    filter: resultMethodId
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.code == 200) {
                    successCallback(response.data.data)
                } else {
                    showErrorDlg('下载出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('下载出错', response.statusText);
            });
            return promise;
        }
    };
    return result;
}]);